var router = require('./menu.js');
var Vue = require('vue/dist/vue.esm.js').default;

router.beforeEach(function(to, from, next) {
    const notLogin = [
        '/main',
        '/login_register',
        '/news',
        '/hash_rate',
        '/coin_haha',
        '/personal_center/get_cash'
    ];
    const userDATA = $.cookie('userDATA') || {};
    let jumpPath = to.path;
    let nowPath = from.path;

    if (notLogin.indexOf(jumpPath) === -1 && !userDATA.id) {             // 如果跳转页面需要登录，cookie中又获取不到用户信息
        const shouldJump = nowPath !== '/login_register';
        Vue.$modal.warning({
            title: '操作错误',
            content: '您还未登录，请先登录！！',
            onOk: function() {
                if (shouldJump) next({ path: '/login_register'});
            },
            cancel: function() {
                if (shouldJump) next({ path: '/login_register'});
            }
        });
    } else {
        next();
    }
    $('#app').scrollTop(0);
});

module.exports = router;